<template>
  <!-- // 分配角色 -->
  <div class="employees-container">
    <div class="app-container">
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
          item.name
        }}</el-checkbox>
      </el-checkbox-group>

      <div style="margin-top: 20px; text-align: right">
        <el-button type="primary" @click="hSave">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting.js'
import { assignRoles } from '@/api/employees.js'
import { getUserDetailById, getUserProfile } from '@/api/user.js'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 保存当前选中的权限列表
      list: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 获取所有角色信息
    async loadRoles() {
      const { data } = await getRoles({ page: 1, pagesize: 100 })
      this.list = data.rows
      const info = await getUserDetailById(this.id)
      // 保存这个员工当前的已经有的角色
      this.roleIds = info.data.roleIds
      console.log(this.roleIds)
    },
    // 获取所有角色信息
    closeDialog() {
      this.$emit('close')
    },
    // 保存当前角色信息
    async hSubmit() {
      await assignRoles({ id: this.id, roleIds: this.roleIds })
      this.$message.success('保存成功')
      // 通知父组件关闭弹出层
      this.$emit('close')
    },
    // 确认保存当前角色
    hSave() {
      this.hSubmit()
      // 如果操控的是当前角色则强制刷新页面
      if (this.$store.state.user.userInfo.id === this.id) {
        location.reload()
      }
    }
  }
}
</script>
